package com.study.zookeeper.impl;

import com.study.common.register.IServiceRegister;
import com.study.zookeeper.IZookeeperRegister;
import lombok.extern.slf4j.Slf4j;
import org.apache.zookeeper.KeeperException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.List;

/**
 * Created by zhangbo on 2020/5/30.
 */
@Slf4j
@Component
public class ServiceRegisterImpl implements IServiceRegister {

    @Autowired
    private IZookeeperRegister zookeeperRegister;


    @Override
    public boolean register(String path, byte[] registerInfo) {
        try {
            boolean register = zookeeperRegister.saveRegister(path, registerInfo);
            return register;
        } catch (IOException e) {
            log.error("服务注册失败：{}", e);
        } catch (KeeperException e) {
            log.error("服务注册失败：{}", e);
        } catch (InterruptedException e) {
            log.error("服务注册失败：{}", e);
            Thread.currentThread().interrupt();
        }
        return false;
    }

    @Override
    public boolean updateRegister(String path, byte[] registerInfo) {
        try {
            boolean register = zookeeperRegister.saveRegister(path, registerInfo);
            return register;
        } catch (IOException e) {
            log.error("更新服务注册信息失败：{}", e);
        } catch (KeeperException e) {
            log.error("更新服务注册信息失败：{}", e);
        } catch (InterruptedException e) {
            log.error("更新服务注册信息失败：{}", e);
            Thread.currentThread().interrupt();
        }
        return false;
    }

    @Override
    public byte[] getRegisterInfo(String path) {
        try {
            byte[] service = zookeeperRegister.getRegister(path);
            return service;
        } catch (IOException e) {
            log.error("获取服务注册信息失败：{}", e);
        } catch (KeeperException e) {
            log.error("获取服务注册信息失败：{}", e);
        } catch (InterruptedException e) {
            log.error("获取服务注册信息失败：{}", e);
            Thread.currentThread().interrupt();
        }
        return null;
    }

    @Override
    public List<String> getRegisterPathInfo(String path) {
        List<String> registerPathList = null;
        try {
            registerPathList = zookeeperRegister.getRegisterPathList(path);
        } catch (IOException e) {
            log.error("获取服务注册信息失败：{}", e);
        } catch (KeeperException e) {
            log.error("获取服务注册信息失败");
        } catch (InterruptedException e) {
            log.error("获取服务注册信息失败：{}", e);
            Thread.currentThread().interrupt();
        }
        return registerPathList;
    }

    @Override
    public boolean deleteRegisterPathInfo(String path) {
        try {
            return zookeeperRegister.deleteRegisterPath(path);
        } catch (IOException e) {
            log.error("服务目录删除失败：{}", e);
        } catch (KeeperException e) {
            log.error("服务目录删除失败：{}", e);
        } catch (InterruptedException e) {
            log.error("服务目录删除失败：{}", e);
            Thread.currentThread().interrupt();
        }
        return false;
    }
}
